/**
 * 
 * @authors Riche
 * @date    2016-07-06 14:39:22
 * @version 1.1.0
 */
var dataID = $.getUrlVar("uid");
var vm = new Vue({
    el: "#app",
    data: {
        title: '',
        addShow: false,
        addphotoshow: false,
        contentPicture: [],
        blocks: [],
        circleID: 0,
        typeID: 0
    },
    compiled: function() {
        var _self = this;
        _self.loadPhoto(); //调用图片预加载
        
        Common.ajaxCall({ url: "ask_getTypeList", datatype: 'json', type: 'get' }, function(data) {
            if (data.error_code == 0) {
                _self.blocks = data.data;
            } else {
                alert(data.msg);
            }
        });

        // 选择图片
        document.getElementById('pic').onchange = function() {
            var img = event.target.files[0];
            // 判断是否图片  
            if (!img) {
                return;
            }
            // 判断图片格式  
            if (!(img.type.indexOf('image') == 0 && img.type && /\.(?:jpg|png|gif)$/.test(img.name))) {
                alert('图片只能是jpg,gif,png');
                return;
            }
            
            //定义图片参数
            var uploadFile = new FormData();
            uploadFile.append("uploadFile", img);
            //var uploadFile = new FormData(img);

            /*//接口方面的更新，现在上传图片必须得有accessToken等
            getaccessToken(function(token) {

                var url = 'ask_getFolderId';
                Common.ajaxCall({
                    url: url,
                    datatype: 'json',
                    type: 'post'
                }, function(data) {
                    if (data.error_code == 0) {
                        uploadFile.append("folderId", data.data[0].folderId);
                        uploadFile.append("accessToken", token);
                        uploadFile.append("tags", _self.title);

                        //data = Common.jsonEncode(data);
                        Common.ajaxSubmitPic({
                            url: 'upload_file',
                            data: uploadFile,
                        }, function(data) {
                            if (data.error_code == 0) {
                                
                                var dom = '<li class="imgBox j_1" ><i class="closeImg" onclick="delImg(this);"></i><img src="' + data.data[0].content + '"  class="loadImg"></li>';
                                $('#dom').append(dom);
                                //$('#pic1').attr('src',image);
                                _self.contentPicture.push(data.msg);
                                myJS.imgScale('.j_1', '1', '1');
                                myJS.ImgFull('.loadImg','images/liby.png');
                                //vm.addReplyPic(image);
                                
                                //添加图片到内容中去
                                //$('#content').htmlarea('tofocus');
                                $('#content').htmlarea('image', data.data[0].content);
                            } else {
                                alert(data.msg);
                            }
                        });
                    } else {
                        alert(data.msg);
                    }
                });
            });*/

            //接口方面的更新，现在上传图片必须得有accessToken等
            getaccessToken(function(token) {

                var url = 'ask_getFolderId';
                Common.ajaxCall({
                    url: url,
                    datatype: 'json',
                    type: 'post'
                }, function(data) {
                    if (data.error_code == 0) {
                        uploadFile.append("folderId", data.data[0].folderId);
                        uploadFile.append("accessToken", token);
                        uploadFile.append("tags", _self.title);

                        //data = Common.jsonEncode(data);
                        Common.ajaxSubmitPic({
                            url: 'upload_file',
                            data: uploadFile,
                        }, function(data) {
                            if (data.error_code == 0) {
                                //添加图片到内容中去
                                /*$('#content').htmlarea('tofocus');
                                $('#content').htmlarea('image', data.data[0].content);*/
                                var dom = '<li class="imgBox"><i class="closeImg" onclick="delImg(this);"></i><span class="photo j_1"><img data-src="'+data.data[0].content+'" data-fileid="'+data.data[0].fileId+'" class="loadImg"></span></li>';
                                $('#dom').append(dom);
                                //$('#pic1').attr('src',image);
                                _self.contentPicture.push(data.data[0].content);
                                _self.loadPhoto();
                                //vm.addReplyPic(image);
                            } else {
                                alert(data.msg);
                            }
                        });
                    } else {
                        alert(data.msg);
                    }
                });
            });


        }

    },
    methods: {
        publish: function() {
            var _self = this;
            var title = $("#title").val();
            var content = $("#content").val();
            
            if (title == "") { //如果没填写内容
                alert('请填写内容');
                $('#title').focus();
                return false;
            }

            var data = {
                uid: dataID,
                type: _self.typeID,
                contentPicture: _self.contentPicture.join(','),
                title: title,
                content: content
            }
            data = Common.jsonEncode(data);
            Common.ajaxCall({ url: 'look_addConsultation', datatype: 'json', type: 'post', data: data }, function(data) {
                if (data.error_code == 0) {
                    alert(data.msg);
                    //延迟跳转
                    setTimeout(function() { window.history.go(-1); }, 500);
                } else {
                    alert(data.msg);
                }
            });
        },
        addphoto: function() {
            if (this.addphotoshow) {
                this.addphotoshow = false;
            } else {
                this.addphotoshow = true;
            }
        },
        loadPhoto: function() {
            myJS.imgScale('.j_1', '1', '1');
            myJS.imgScale('.j_1511', '15', '11');
            myJS.ImgFull('.loadImg', 'images/liby.png');
        },
        selectPic: function() {
            //选择图片
            $("#pic").click();
        }
    },
    watch: {
        "willShow": function() {
            this.loadPhoto();
        },
        "blocks": function() {
            var _self = this;
            //初始化mobilescroll组件
            var opt = {
                'default': {
                    theme: 'default',
                    mode: 'scroller',
                    display: 'bottom',
                    animate: 'fade',
                    placeholder: '选择板块',
                    //交换按钮位置
                    buttons: [
                        'set'
                    ],

                    headerText: function(valueText) { return "选择板块"; },
                    // 选择之后赋值
                    onSelect: function(e) {

                        _self.title = $('#names_dummy').val();
                    }

                },
                'select-opt': {
                    preset: 'select',
                    group: false
                }
            }
            $('.block-select-opt').scroller($.extend(opt['select-opt'], opt['default']));
            _self.typeID = $('#names').children('option').eq(0).val();
            $('#names').change(function() {
                _self.typeID = $(this).children('option:selected').val(); //拿到value值
            });
            _self.title = $('#names_dummy').val(); //初始化赋值

        }
    }
});